package com.kyzh.core.pager.weal.accountbuy

import android.os.Bundle
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.chad.library.adapter.base.BaseNodeAdapter
import com.chad.library.adapter.base.entity.node.BaseNode
import com.gushenge.core.base.activity.BaseActivity
import com.gushenge.core.beans.Small
import com.gushenge.core.impls.UserImpl
import com.gushenge.core.listeners.ResultListener
import com.kyzh.core.R
import com.kyzh.core.adapters.providers.SmallItemProvider
import com.kyzh.core.adapters.providers.SmallProvider
import com.kyzh.core.uis.recyclerView
import com.kyzh.core.uis.swipeRefreshLayout
import com.kyzh.core.uis.titlebar
import kotlinx.android.synthetic.main.activity_small_title.*
import org.jetbrains.anko.include
import org.jetbrains.anko.matchParent
import org.jetbrains.anko.toast
import org.jetbrains.anko.verticalLayout

class SmallActivity : BaseActivity() {

    lateinit var recyclerView: RecyclerView
    lateinit var root: SwipeRefreshLayout

    private val adapter1 = SmallAdapter()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initView()
    }

    private fun initView() {

        verticalLayout {
            titlebar(getString(R.string.smallManage)).init(this, this@SmallActivity)
            include<ConstraintLayout>(R.layout.activity_small_title)
            root = swipeRefreshLayout {
                setOnRefreshListener {
                    addData()
                }
                recyclerView = recyclerView {
                }.lparams(matchParent, matchParent)
            }.lparams(matchParent, matchParent)
        }

        initUi()

    }

    private fun addData() {
        root.isRefreshing = true
        UserImpl.small(object : ResultListener {
            override fun success(bean: Any) {
                recyclerView.apply {
                    layoutManager = LinearLayoutManager(this@SmallActivity)
                    adapter = adapter1
                    adapter1.setList(bean as MutableList<Small>)
                }

                root.isRefreshing = false
            }

            override fun error(error: String) {
                toast(error)
                root.isRefreshing = false
            }
        })
    }

    private fun initUi() {
        tvTime.text = getString(R.string.smallName)
        tvPoint.text = getString(R.string.registerTime)
        tvSummary.text = getString(R.string.operating)
    }

    override fun onResume() {
        super.onResume()
        addData()
    }
    private inner class SmallAdapter : BaseNodeAdapter() {

        init {
            addNodeProvider(SmallProvider())
            addNodeProvider(SmallItemProvider())
        }


        override fun getItemType(data: List<BaseNode>, position: Int): Int {
            val node = data[position]
            return if (node is Small){
                0
            }else{
                1
            }
        }

    }
}
